/***************************************************************************\
 * The Mattise Kernel														*
 * Copyright 2007 Matthew Iselin											*
 * Licensed under the GPL													*
 *																			*
 * system.h																	*
 *																			*
 * System core functions, defines and types.								*
 *																			*
\***************************************************************************/

#ifndef SYSTEM_H
#define SYSTEM_H

/* This defines what the stack looks like after an ISR was running */
struct regs
{
    unsigned int gs, fs, es, ds;      /* pushed the segs last */
    unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;  /* pushed by 'pusha' */
    unsigned int int_no, err_code;    /* our 'push byte #' and ecodes do this */
    unsigned int eip, cs, eflags, useresp, ss;   /* pushed by the processor automatically */ 
};

// asm macros
#define INTELSYNTAX ( asm ( ".intel_syntax noprefix" ) )
#define ATTSYNTAX	( asm ( ".att_syntax" ) )

// installs the GDT
void gdt_install();

// installs the IDT
void idt_install();

// install the ISRs
void isrs_install();

// sets up IRQ mapping
void irq_install();

// installs the timer
void install_timer();

// installs an IRQ handler (will eventually allow shared irqs)
void irq_install_handler(int irq, void (*handler)(struct regs *r));

// uninstalls an IRQ handler
void irq_uninstall_handler(int irq);

// ACCESSORY FUNCTIONS //

// sets a gate in the IDT
void idt_set_gate(unsigned char num, unsigned long base, unsigned short sel, unsigned char flags);

// port out
void outportb( short port, char dat );
void outportw( short port, short dat );
void outportl( short port, int dat );

// port in
char inportb( short port );
short inportw( short port );
int inportl( short port );

// CR register manipulation
unsigned int read_cr0();
void write_cr0( unsigned int cr0 );

// calls a real-mode interrupt
void int_v86( int intnum );

// easy to type
typedef unsigned char uchar_t;
typedef unsigned short ushort_t;
typedef unsigned int uint_t;
typedef unsigned long ulong_t;
typedef uchar_t uint8_t;
typedef ushort_t uint16_t;
typedef uint_t uint32_t;
typedef ulong_t uint64_t;

// define null!
#ifndef NULL
#define NULL ((void*)0)
#endif

#endif
